package com.panda.framework.netty.handler.socket;

import Jubo.JuLiao.IM.Wx.Proto.FriendAddNotice.FriendAddNoticeMessage;
import Jubo.JuLiao.IM.Wx.Proto.FriendAddNotice.FriendMessage;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumErrorCode;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumMsgType;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.TransportMessage;
import com.panda.business.domain.WeChatAccountInfo;
import com.panda.business.domain.WeChatContactInfo;
import com.panda.business.service.WeChatAccountService;
import com.panda.business.service.WeChatContactService;
import com.panda.framework.netty.async.AsyncTaskService;
import com.panda.framework.netty.common.Constant;
import com.panda.framework.netty.utils.MessageUtil;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


@Service
public class FriendAddNoticeHandler{
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private WeChatAccountService weChatAccountService;
	@Autowired
	private AsyncTaskService asyncTaskService;
	@Autowired
	private WeChatContactService  weChatContactService;
	 
	/**
	 * 个人号新增好友通知消息---手机端经过服务端转发给pc端
	 * @author wechatno:tangjinjinwx
	 * @param ctx
	 * @param vo
	 */
	@Async
    public  void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
        try {
        	FriendAddNoticeMessage req = vo.getContent().unpack(FriendAddNoticeMessage.class);
        	 
    		//把消息转发给pc端
    		//a、根据wechatId找到accountid
    		//b、通过accountid找到account
    		//c、通过account账号找到通道
    		WeChatAccountInfo  account = weChatAccountService.findWeChatAccountInfoByWeChatId(req.getWeChatId());
    		  
    		asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.FriendAddNotice, req);
    		
    		
    		//告诉客户端消息已收到
    		MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
    		  
			//保存新增好友
    		if(null != account){
    			saveFriendContactinfo(weChatContactService, req, account);
    		}
        } catch (Exception e) {
            e.printStackTrace();
            MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), Constant.ERROR_MSG_DECODFAIL);
        }
    }
    
    private static void saveFriendContactinfo(WeChatContactService  weChatContactService,FriendAddNoticeMessage req,WeChatAccountInfo  account){
    	try {
			//新增好友
			FriendMessage  friend = req.getFriend();
			WeChatContactInfo   contactinfo = weChatContactService.findContactinfoByfriendid(account.getCid(),req.getWeChatId(),friend.getFriendId());
			if(null != contactinfo){
				setContActinfo(contactinfo, friend);
				weChatContactService.update(contactinfo);
			}else{
				contactinfo = new WeChatContactInfo();
				setContActinfo(contactinfo, friend);
				contactinfo.setCid(account.getCid());
				contactinfo.setWechatid(req.getWeChatId());
				contactinfo.setFriendid(friend.getFriendId());
				weChatContactService.insert(contactinfo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    private  static void  setContActinfo(WeChatContactInfo   contactinfo,FriendMessage  friend){
    	contactinfo.setFriend_wechatno(friend.getFriendNo());
		contactinfo.setNickname(friend.getFriendNick());
		contactinfo.setGender(friend.getGenderValue());
		contactinfo.setAvatar(friend.getAvatar());
		contactinfo.setCountry(friend.getCountry());
		contactinfo.setProvince(friend.getProvince());
		contactinfo.setCity(friend.getCity());
		contactinfo.setState(0);
    }
}